<template>
  <div class="default-main">
    <el-row :gutter="20">
      <!-- 字典管理 -->
      <el-col class="xs-mb-20" :xs="24" :sm="24" :lg="12">
        <el-card shadow="always" header="字典数据">
          <Dictionary @on-show-dict-list="showList" />
        </el-card>
      </el-col>
      <!-- 字典详情列表 -->
      <el-col class="xs-mb-20" :xs="24" :sm="24" :lg="12">
        <el-card shadow="always" header="字典详细">
          <template #header>
            <div class="card-header">
              <span>字典项:{{ state.row.dictName }}</span>
              <el-button
                v-if="state.dictList"
                v-blur
                @click="onAction"
                class="table-header-operate"
                type="primary"
              >
                <el-icon>
                  <Plus />
                </el-icon>
                <span class="table-header-operate-text">添加</span>
              </el-button>
            </div>
          </template>
          <DictData v-if="state.dictList" @on-put="openPut" />
          <div v-else>请选择某一字典数据</div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 编辑字典项表单 -->
    <popupForm v-model="state.showAddForm" />
  </div>
</template>
<script lang="ts" setup>
import { reactive, provide } from "vue";
import Dictionary from "./components/dictionary.vue";
import DictData from "./components/dictData.vue";
import popupForm from "./components/popupForm.vue";
import { useDictionary } from "@/store/backend/tool/dictionary";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { DictOperation } from "@/utils/common";

const dict = useDictionary();
const { proxy } = useCurrentInstance(); //mitt
//初始化字典处理类
const dictOperation = new DictOperation();

const state: {
  dictList: boolean;
  row: anyObj;
  showAddForm: boolean;
} = reactive({ dictList: false, row: {}, showAddForm: false });

//字典数据点击编辑获取字典项
const showList = (row: any) => {
  state.dictList = true;
  state.row = row;
};

//打开编辑字典项表单
const openPut = (data: any) => {
  state.showAddForm = true;
  proxy.eventBus.emit("editDict", data);
};
//打开添加字典项表单
const onAction = () => {
  dict.opera = "add";
  proxy.eventBus.emit("addDict", { dictType: state.row.dictType });
  state.showAddForm = true;
};

provide("dictOperation", dictOperation);
</script>
<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
@media screen and (max-width: 768px) {
  .xs-mb-20 {
    margin-bottom: 20px;
  }
}
</style>
